﻿using System.ComponentModel;
using System.ComponentModel.Design;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PrismFactory.SoftwareFactory.Views.GenerateViewClasses;
using Rhino.Mocks;

namespace PrismGuidancePackage.Views.Tests
{
    [TestClass]
    public class GenerateViewClassesPageViewModelFixture
    {
        private GenerateViewClassesPageViewModel viewModel;
        private IDictionaryService stubDictionaryService;

        [TestInitialize]
        public void Setup()
        {
            stubDictionaryService = MockRepository.GenerateStub<IDictionaryService>();
            viewModel = new GenerateViewClassesPageViewModel((stubDictionaryService));
        }

        [TestMethod]
        public void ValidWhenViewNameIsNotNull()
        {
            stubDictionaryService.Stub(s => s.GetValue(GenerateViewClassesPageViewModel.ViewNameKey)).Return("MyChild");
            Assert.IsNull((viewModel as IDataErrorInfo)[GenerateViewClassesPageViewModel.ViewNameKey]);
        }

        [TestMethod]
        public void InvalidWhenViewNameIsNull()
        {
            stubDictionaryService.Stub(s => s.GetValue(GenerateViewClassesPageViewModel.ViewNameKey)).Return(null);
            Assert.IsNotNull((viewModel as IDataErrorInfo)[GenerateViewClassesPageViewModel.ViewNameKey]);
        }

        [TestMethod]
        public void InvalidWhenViewNameIsEmpty()
        {
            stubDictionaryService.Stub(s => s.GetValue(GenerateViewClassesPageViewModel.ViewNameKey)).Return("");
            Assert.IsNotNull((viewModel as IDataErrorInfo)[GenerateViewClassesPageViewModel.ViewNameKey]);
        }

        [TestMethod]
        public void InvalidWhenViewNameIsNotProperlyFormed1()
        {
            stubDictionaryService.Stub(s => s.GetValue(GenerateViewClassesPageViewModel.ViewNameKey)).Return("Invalid Name");
            Assert.IsNotNull((viewModel as IDataErrorInfo)[GenerateViewClassesPageViewModel.ViewNameKey]);
        }

        [TestMethod]
        public void InvalidWhenViewNameIsNotProperlyFormed2()
        {
            stubDictionaryService.Stub(s => s.GetValue(GenerateViewClassesPageViewModel.ViewNameKey)).Return("Invalid.Name");
            Assert.IsNotNull((viewModel as IDataErrorInfo)[GenerateViewClassesPageViewModel.ViewNameKey]);
        }

        [TestMethod]
        public void InvalidWhenViewNameIsNotProperlyFormed3()
        {
            stubDictionaryService.Stub(s => s.GetValue(GenerateViewClassesPageViewModel.ViewNameKey)).Return("Invalid%Name");
            Assert.IsNotNull((viewModel as IDataErrorInfo)[GenerateViewClassesPageViewModel.ViewNameKey]);
        }

        [TestMethod]
        public void InvalidWhenViewNameIsNotProperlyFormed4()
        {
            stubDictionaryService.Stub(s => s.GetValue(GenerateViewClassesPageViewModel.ViewNameKey)).Return("1Invalid.Name");
            Assert.IsNotNull((viewModel as IDataErrorInfo)[GenerateViewClassesPageViewModel.ViewNameKey]);
        }

        [TestMethod]
        public void SetOtherClassesNamesFromViewName()
        {
            const string viewName = "MyView";
            const string viewModelName = "MyViewModel";
            stubDictionaryService.Stub(s => s.GetValue(GenerateViewClassesPageViewModel.ViewNameKey)).Return(viewName);
            stubDictionaryService.Stub(s => s.GetValue(GenerateViewClassesPageViewModel.ViewModelNameKey)).Return(viewModelName);
            viewModel.ViewName = viewName;
            stubDictionaryService.AssertWasCalled(s => s.SetValue(GenerateViewClassesPageViewModel.ViewModelNameKey, viewModelName));
            stubDictionaryService.AssertWasCalled(s => s.SetValue(GenerateViewClassesPageViewModel.ViewModelInterfaceNameKey, "IMyViewModel"));
            stubDictionaryService.AssertWasCalled(s => s.SetValue(GenerateViewClassesPageViewModel.ViewModelTestClassNameKey, "MyViewModelFixture"));
        }
    }
}